// index.js
// 获取应用实例
import { config } from "../../config/config";
// import { Init } from "../../model/init";
import { Http } from "../../utils/http";
import { cache } from "../../utils/util";
const app = getApp()

Page({
  data: {
    order_no: '',
  },


  getPhoneNumber: function (e) {
    console.log(e.detail);
  },

  getCode() {
    wx.login({
      success: (res) => {
        console.log(res.code);
      },
    })
  },
  async onLoad() {
    this.init_();
  },

  init_: async function () {
    let code = await Http.login();
    console.log(code);
    let detail = await Http.request({
      url: 'wechat/product/get_detail_by_id',
      data: { id: 4 },
    })
    this.setData({
      detail: detail,
    })
  },

  getUserMobile(e) {
    let scene = e.currentTarget.dataset.id
    console.log(scene);
    if (scene == 999) {
      var name = 'James Bourne';
    }
    console.log(name);
  },

  placeOrder() {
    wx.request({
      url: `${config.apiBaseUrl}wechat/order/place_order`,
      data: {
        product_id: 16,
        store_id: 1,
        distributor_id: 1,
        username: '谢志雄',
        mobile: '15974708423',
        profit_sharing: 'Y',
      },
      header: {
        token: wx.getStorageSync('token'),
      },
      success: (res) => {
        if (res.data.code == 0) {
          console.log(res.data.data);
          this.setData({
            order_no: res.data.data.order_no,
          })
          cache('order_no_a', res.data.data.order_no);
          console.log(this.data.order_no);
        } else {
          console.log(res);
          console.log(res.data.msg);

          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'error',
          //   duration: 2000,
          // })
        }

      }
    })
  },

  placeOrderCard() {
    wx.request({
      url: `${config.apiBaseUrl}wechat/order/place_order_card`,
      data: {
        product_id: 9,
        store_id: 1,
        distributor_id: 1,
        username: '谢志雄',
        mobile: '15974708423',
        profit_sharing: 'Y',
      },
      header: {
        token: wx.getStorageSync('token'),
      },
      success: (res) => {
        if (res.data.code == 0) {
          console.log(res.data.data);
          this.setData({
            order_no: res.data.data.order_no,
          })

          console.log(this.data.order_no);
        } else {
          console.log(res);
          console.log(res.data.msg);
        }

      }
    })
  },


  getPayment() {
    console.log(this.data.order_no);

    wx.request({
      url: `${config.apiBaseUrl}wechat/pay/unify_pay`,
      data: {
        // order_no: this.data.order_no
        order_no: cache('order_no_a')
      },
      header: {
        token: wx.getStorageSync('token'),
      },
      success: (res) => {
        console.log(res);
        let config = res.data.data;
        wx.requestPayment({
          timeStamp: config.timeStamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: 'MD5',
          paySign: config.paySign,
          success(res) {
            wx.navigateTo({
              // url: '../share/index?product_id=4',
              url: './index',
            })
          },
          fail(res) {}
        })
      }
    });





  },
  placeOrderVirtual: async function () {
    let product_virtual_id = 13;
    let type = 1;
    let result = await Http.request({
      url: 'wechat/order_virtual/place_order',
      data: {
        product_virtual_id: product_virtual_id,
        type: type
      },
    })
    console.log(result.data.order_no);
    cache('order_no', result.data.order_no)
  },

  getPaymentVirtual: async function () {
    let order_no = cache('order_no')
    let result = await Http.request({
      url: 'wechat/pay/pay_virtual',
      data: { order_no: order_no }
    })
    console.log(result);
    let config = result.data;
    wx.requestPayment({
      timeStamp: config.timeStamp,
      nonceStr: config.nonceStr,
      package: config.package,
      signType: 'MD5',
      paySign: config.paySign,
      success(res) {
        wx.navigateTo({
          url: '../share/index?product_id=4',
        })
      },
      fail(res) {}
    })
  },

  getProfile() {
    console.log('getProfile');
    wx.getUserProfile({
      desc: '获取用户的Profile',
      success: (res) => {
        console.log(res);
      },
    })
  },

  getInfo() {
    console.log('getInfo');
    wx.getUserInfo({
      desc: 'Hello',
      success: (res) => {
        console.log(res);
      },
      fail: (res) => {
        console.log(res);
      },
    })
  },

  navigateUser() {
    wx.navigateTo({
      url: '/pages/user/index',
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: async function (query) {
    console.log(this.data.detail.data.share_image);
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '自定义标题AAAAA',
        })
      }, 2000)
    })
    return {
      from: 'button',
      // title: this.data.detail.data.name,
      title: this.data.detail.data.name != "Rose" ? this.data.detail.data.name : "Hello",
      path: 'pages/distributor/index?scene=55,4',
      imageUrl: this.data.detail.data.share_image,
    }
  },

  onRefresh: async function () {
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '刷新中 ...',
    })
  },

  onPullDownRefresh: function () {
    console.log('触发下拉刷新函数');
  }
})